package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     *
     * @param dto
     * @return
     */
    @Override
    public Result add(CategoryDTO dto) {
        //非空判断
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //判断分类名称是否存在
        Category category = categoryMapper.selectByUserName(dto.getName());
        if (category != null) {
            return Result.error(MessageConstant.USER_ALREADY_EXISTS);
        }
        //属性拷贝
        Category category1 = new Category();
        BeanUtils.copyProperties(dto, category1);
        //设置状态,默认是禁用
        category1.setStatus(StatusConstant.DISABLE);
        //设置创建时间和创建人的ID
        // category1.setCreateTime(LocalDateTime.now());
        // category1.setCreateUser(BaseContext.getCurrentId());
        // //设置修改时间和修改人ID
        // category1.setUpdateTime(LocalDateTime.now());
        // category1.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper方法
        categoryMapper.add(category1);


        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO dto) {
        //非空判断
        if (dto.getPage() == null) {
            //设置默认值
            dto.setPage(1);
        }
        if (dto.getPageSize() == null) {
            dto.setPageSize(10);
        }
        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用mapper
        Page<Category> page = categoryMapper.page(dto);
        //封装数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        //返回
        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {
        //判断非空
        if (id == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //查询当前分类是否关联了菜品
        Integer count = dishMapper.countByCategoryId(id);
        if (count != null) {
            //如果不为空,说明关联了菜品 抛出异常
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询但前分类是否关联了套餐
        Integer count2 = setmealMapper.countByCategoryId(id);
        if (count2 != null) {
            //如果不为空,说明关联了套餐 抛出异常
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //调用mapper
        categoryMapper.deleteById(id);
        //返回
        return Result.success();
    }

    /**
     * 根据id查询分类(数据回显)
     *
     * @param id
     * @return
     */
    @Override
    public Result selectById(Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //调用mapper
        Category category = categoryMapper.selectById(id);

        return Result.success(category);
    }

    /**
     * 修改分类
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateCategory(CategoryDTO dto) {
        //非空判断
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //判断分类名是否存在
        Category name = categoryMapper.selectByUserName(dto.getName());
        if (name != null) {
            return Result.error(MessageConstant.USER_ALREADY_EXISTS);
        }
        //拷贝属性
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        //设置修改人ID和修改时间
        // category.setUpdateUser(BaseContext.getCurrentId());
        // category.setUpdateTime(LocalDateTime.now());
        //调用mapper
        categoryMapper.updateCategory(category);
        return Result.success();
    }

    /**
     * 启用禁用
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(Integer status, Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        if (status == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //设置修改人ID和修改时间
        Category category = new Category();
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper
        categoryMapper.updateStatus(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> selectByType(Integer type) {
        //判断非空
        // if (type == null) {
        //     return Result.error(MessageConstant.UNKNOWN_ERROR);
        // }
        //开启分页查询
        //   PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //   //调用mapper
        //
        // Page<Category> categories=  categoryMapper.selectByType(dto);
        //
        //   PageResult pageResult = new PageResult();
        //   pageResult.setTotal(categories.getTotal());
        //   pageResult.setRecords(categories.getResult());
        List<Category> categories = categoryMapper.selectByType(type);

        return Result.success(categories);
    }


}
